package com.beehoo.dcportal.dao.impl;

import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.dao.IMessageDao;
import com.beehoo.dcportal.dao.IMessageDao.CoverType;
import com.beehoo.dcportal.dao.IMessageDao.MsgStatus;
import com.beehoo.dcportal.model.MessageBean;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

public class MessageDaoImpl extends BaseDao
  implements IMessageDao
{
  private static final Logger logger = Logger.getLogger(MessageDaoImpl.class);

  public List<MessageBean> getMessageList(String streetId, String msgType, IMessageDao.MsgStatus msgState, IMessageDao.CoverType cover, int iPageNo, int iRows)
    throws BaseException
  {
    List msgList = new ArrayList();
    Map paramMap = new HashMap();
    paramMap.put("streetId", streetId);
    paramMap.put("msgType", msgType);
    paramMap.put("msgState", msgState.val());
    paramMap.put("coverType", cover.val());
    paramMap.put("msgRows", Integer.valueOf(iRows));
    paramMap.put("offset", Integer.valueOf((iPageNo - 1) * iRows));
    try
    {
      msgList = this.sqlMapClient.queryForList("portal-message.queryMsgList", paramMap);
      logger.info("信息列表-获取信息列表|#调用portal-message.queryMsgList 中心编号：" + streetId + "信息类型：" + msgType + "信息状态：" + msgState.val() + "封面状态：" + cover.val() + "查询页码：" + iPageNo + "查询条数：" + iRows + ",成功!");
    }
    catch (SQLException e)
    {
      logger.error("信息列表-获取信息列表|#调用portal-message.queryMsgList 中心编号：" + streetId + "信息类型：" + msgType + "信息状态：" + msgState.val() + "封面状态：" + cover.val() + "查询页码：" + iPageNo + "查询条数：" + iRows + ",失败!", e);

      throw new BaseException(e, "获取信息列表失败");
    }

    return msgList;
  }

  public List<MessageBean> getPortletMessageList(String streetId, String msgType, IMessageDao.MsgStatus msgState, IMessageDao.CoverType cover, int iPageNo, int iRows)
    throws BaseException
  {
    List msgList = new ArrayList();
    Map paramMap = new HashMap();
    paramMap.put("streetId", streetId);
    paramMap.put("msgType", msgType);
    paramMap.put("msgState", msgState.val());
    paramMap.put("coverType", cover.val());
    paramMap.put("msgRows", Integer.valueOf(iRows));
    paramMap.put("offset", Integer.valueOf((iPageNo - 1) * iRows));
    try
    {
      msgList = this.sqlMapClient.queryForList("portal-message.queryPortletMsgList", paramMap);
      logger.info("信息列表-获取信息列表|#调用portal-message.queryMsgList 中心编号：" + streetId + "信息类型：" + msgType + "信息状态：" + msgState.val() + "封面状态：" + cover.val() + "查询页码：" + iPageNo + "查询条数：" + iRows + ",成功!");
    }
    catch (SQLException e)
    {
      logger.error("信息列表-获取信息列表|#调用portal-message.queryMsgList 中心编号：" + streetId + "信息类型：" + msgType + "信息状态：" + msgState.val() + "封面状态：" + cover.val() + "查询页码：" + iPageNo + "查询条数：" + iRows + ",失败!", e);

      throw new BaseException(e, "获取信息列表失败");
    }

    return msgList;
  }

  public int getMessageCount(String streetId, String msgType, IMessageDao.MsgStatus msgState, IMessageDao.CoverType cover)
    throws BaseException
  {
    int msgCount = 0;
    Map paramMap = new HashMap();
    paramMap.put("streetId", streetId);
    paramMap.put("msgType", msgType);
    paramMap.put("msgState", msgState.val());
    paramMap.put("cover", cover.val());
    try
    {
      msgCount = ((Integer)this.sqlMapClient.queryForObject("portal-message.getMsgCount", paramMap)).intValue();
      logger.info("信息列表-获取信息总数|#调用portal-message.getMsgCount 中心编号：" + streetId + "信息状态：" + msgState.val() + "封面状态：" + cover.val() + "信息类型：" + msgType + ",成功!");
    }
    catch (SQLException e)
    {
      logger.error("信息列表-获取信息总数|#调用portal-message.getMsgCount 中心编号：" + streetId + "信息状态：" + msgState.val() + "封面状态：" + cover.val() + "信息类型：" + msgType + ",失败!", e);

      throw new BaseException(e, "获取信息总数失败");
    }

    return msgCount;
  }

  public MessageBean getMessageItem(String streetId, String msgId)
    throws BaseException
  {
    MessageBean message = new MessageBean();
    Map paramMap = new HashMap();
    paramMap.put("streetId", streetId);
    paramMap.put("msgId", msgId);
    try
    {
      message = (MessageBean)this.sqlMapClient.queryForObject("portal-message.getMsgDetail", paramMap);
      logger.info("信息详细-获取信息详细|#调用portal-message.getMsgDetail 中心编号：" + streetId + "信息编号：" + msgId + ",成功!");
    }
    catch (SQLException e)
    {
      logger.error("信息详细-获取信息详细|#调用portal-message.getMsgDetail 中心编号：" + streetId + "信息编号：" + msgId + ",失败!", e);

      throw new BaseException(e, "获取信息详细失败");
    }

    return message;
  }
}